package com.plm.mr.writable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

    private FlowBean out = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        // 1.遍历values，将其中的上行流量和下行流量分别累加
        long totalUp = 0;
        long totalDown = 0;
        for (FlowBean flowBean : values) {
            totalUp += flowBean.getUpFlow();
            totalDown += flowBean.getDownFlow();
        }

        // 2.封装输出out
        out.setUpFlow(totalUp);
        out.setDownFlow(totalDown);
        out.setSumFlow();

        // 3.写出
        context.write(key, out);
    }

}
